import os

def rename(path):
    i = 0
    '该文件夹下所有的文件（包括文件夹）'
    FileList = os.listdir(path)
    '遍历所有文件'
    for file in FileList:
        '原来的文件路径'
        oldDirPath = os.path.join(path, file)
        '如果是文件夹则递归调用'
        if os.path.isdir(oldDirPath):
            rename(oldDirPath)

        # print(files)
        if len(file.split(' '))>0:
            os.rename(os.path.join(path,file)
                      ,os.path.join(path,file.replace(' ','_')))

        i += 1
    print(f'次数：{i}')

def find_all_file(path):
    '该文件夹下所有的文件（包括文件夹）'
    FileList = os.listdir(path)
    '遍历所有文件'
    for file in FileList:
        '原来的文件路径'
        oldDirPath = os.path.join(path, file)
        '如果是文件夹则递归调用'
        if os.path.isdir(oldDirPath):
            find_all_file(oldDirPath)

        print(file)



# if __name__ == '__main__':
    # path = 'F:\\'
    # rename(path)
    # find_all_file()